package leetcode.D700.T654;

import util.TreeNode;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-15 15:57:03 星期二
 */

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums, 0, nums.length-1);
    }

    private TreeNode build(int[] nums, int left, int right) {
        if (left > right)
            return null;
        int max = 0, k = left;
        for(int i=left; i<=right; ++i) {
            if (nums[i] > max) {
                max = nums[i];
                k = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = build(nums, left, k-1);
        root.right = build(nums, k+1, right);
        return root;
    }
}